package com.woniuxy.project.campus.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project.campus.account.dto.GetAllAuthorityName;
import com.woniuxy.project.campus.account.mapper.AccountMapper;
import com.woniuxy.project.campus.account.mapper.RoleAccountMapper;
import com.woniuxy.project.campus.account.model.Account;
import com.woniuxy.project.campus.account.model.Role;
import com.woniuxy.project.campus.account.mapper.RoleMapper;
import com.woniuxy.project.campus.account.model.RoleAccount;
import com.woniuxy.project.campus.account.param.UpdateUserAuthorityNameParam;
import com.woniuxy.project.campus.account.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.project.campus.util.TokenTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年02月17日
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private RoleAccountMapper roleAccountMapper;
    @Resource
    private RoleMapper roleMapper;
    @Override
    public List<GetAllAuthorityName> getAllAuthorityName(String token) {
        //解析token 获取用户id
        Integer id = TokenTool.getUserIdByToken(token);
        //判断是否为超级管理员
        Account account = accountMapper.selectById(id);
        if(account==null){
            throw new RuntimeException("用户不存在");
        }
        QueryWrapper<RoleAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",1);
        queryWrapper.eq("account_id",id);
        RoleAccount roleAccount = roleAccountMapper.selectOne(queryWrapper);
        if(roleAccount==null){
            throw new RuntimeException("无权限查看");
        }
        List<GetAllAuthorityName> authorityNames = roleAccountMapper.selectAllAuthorityName();
        return authorityNames;
    }

    @Override
    public void updateUserAuthorityName(UpdateUserAuthorityNameParam bean) {
        //解析token 获取用户id
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        //判断是否为超级管理员
        Account account = accountMapper.selectById(id);
        if(account==null){
            throw new RuntimeException("用户不存在");
        }
        QueryWrapper<RoleAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",1);
        queryWrapper.eq("account_id",id);
        RoleAccount roleAccount = roleAccountMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        if(roleAccount==null){
            throw new RuntimeException("无权限查看");
        }
        //查询用户权限的角色
       QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",bean.getUserId());
        queryWrapper1.eq("role_id",bean.getRoleId());
        Account account1 = accountMapper.selectOne(queryWrapper1);
        if(account1==null){
            throw new RuntimeException("用户不存在");
        }
        if(account1.getRoleName().equals(bean.getRoleName())){
            throw new RuntimeException("不能与原有角色名称相同");
        }
        //查询修改的角色名
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        System.out.println(bean.getRoleName());
        queryWrapper2.eq("name",bean.getRoleName());
        Role role = roleMapper.selectOne(queryWrapper2);
        if(role==null){
            throw new RuntimeException("角色名不存在");
        }
        queryWrapper2.clear();
        QueryWrapper<RoleAccount> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("account_id",bean.getUserId());
        RoleAccount roleAccounts = roleAccountMapper.selectOne(queryWrapper3);
        if(roleAccounts==null){
            throw new RuntimeException("用户无角色");
        }

        roleAccounts.setRoleId(role.getId());
        roleAccountMapper.updateById(roleAccounts);

        account1.setRoleName(bean.getRoleName());
        account1.setRoleId(role.getId());
        accountMapper.updateById(account1);

    }
}
